package com.dxg.msns.gateway.config;

import com.dxg.msns.auth.utils.RsaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.security.PublicKey;

@ConfigurationProperties(prefix = "msns.jwt")
public class JwtProperties {
    private static final Logger logger = LoggerFactory.getLogger(JwtProperties.class);

    private String pubKeyPath; // 公钥
    private PublicKey publicKey;
    private String cookieName;

    @PostConstruct
    public void init() {
        try {
            if (pubKeyPath != null && !pubKeyPath.isEmpty()) {
                this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
                 System.out.println("Successfully loaded public key from: {}"+ pubKeyPath);
            } else {
                logger.warn("Public key path is not configured");
            }
        } catch (Exception e) {
             System.out.println("Failed to load public key from: " + pubKeyPath+e);
            // 不要在这里抛出异常，让应用继续启动
        }
    }

    public String getPubKeyPath() {
        return pubKeyPath;
    }

    public void setPubKeyPath(String pubKeyPath) {
        this.pubKeyPath = pubKeyPath;
    }

    public PublicKey getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
    }

    public String getCookieName() {
        return cookieName;
    }

    public void setCookieName(String cookieName) {
        this.cookieName = cookieName;
    }
}
